package com.sams.service;

import com.sams.dao.DiseaseControlMapper;
import com.sams.dao.MedicationHistoryMapper;
import com.sams.dao.SubjectMapper;
import com.sams.pojo.DiseaseControl;
import com.sams.pojo.PageBean;
import com.sams.pojo.Query1;
import com.sams.pojo.Subject;
import com.sams.util.SqlSF_tools;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class DiseaseControlService {
    private SqlSessionFactory sqlSessionFactory= SqlSF_tools.getSqlSessionFactory();;
    private SqlSession sqlSession;
    public PageBean<DiseaseControl> selectbyPage(int currentPage, int pageSize, Query1 query1) {
        sqlSession = sqlSessionFactory.openSession(true);
        DiseaseControlMapper mapper = sqlSession.getMapper(DiseaseControlMapper.class);
        /*计算*/
        int begin = (currentPage - 1) * pageSize;
        int size = pageSize;
        List<DiseaseControl> rows = mapper.selectBypage(begin, size, query1);
        int totalCount = mapper.selectTotalCount();
        PageBean<DiseaseControl> pageBean=new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        sqlSession.close();
        return pageBean;
    }

    public boolean addDisease(DiseaseControl diseaseControl) {
        sqlSession = sqlSessionFactory.openSession(true);
        DiseaseControlMapper mapper = sqlSession.getMapper(DiseaseControlMapper.class);
        int add = mapper.addData(diseaseControl);
        if (add != 0 && diseaseControl != null) {
            sqlSession.close();
            return true;
        } else {
            sqlSession.close();
            return false;
        }
    }

    public boolean deleteById(String deleteSubjectid) {
        sqlSession = sqlSessionFactory.openSession(true);
        DiseaseControlMapper mapper = sqlSession.getMapper(DiseaseControlMapper.class);
        int deletedata = mapper.deletedata(deleteSubjectid);
        if (deletedata != 0) {
            sqlSession.close();
            return true;
        } else {
            sqlSession.close();
            return false;
        }
    }
}
